﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class SLANew : ICPRelationshipPage
{
    public SLAData slaData = new SLAData();
    #region Properties
    
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, SLA - Create New";
        if (!IsPostBack && !IsCallback)
        {
        }
    }
    protected void cbkSLANew_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = CreateNewSLA(e.Parameter);
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    private string CreateNewSLA(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxReturn = String.Empty;

        foreach (String sxRow in sxSplitted)
        {
            String[] sxRowSplitted = sxRow.Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);

            if (sxRowSplitted[0].ToUpper().Contains(KSLA.CP_ID))
            {
                String[] sxIDSplitted = sxRowSplitted[1].Split('_');
                bool IsNewCP = sxIDSplitted[1].ToUpper() == KGlobal.YES;

                int iCPId = Convert.ToInt32(sxIDSplitted[0]);
                if (IsNewCP)
                {
                    slaData.iNewContactPersonID = iCPId;
                }
                else
                {
                    slaData.iContactPersonID = iCPId;
                }
                
                continue;
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KSLA.SLA_MONTH))
            {
                slaData.iMonth = Convert.ToInt32(sxRowSplitted[1]);
                slaData.sxMonth = sxRowSplitted[1];
                continue;
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KSLA.SLA_YEAR))
            {
                slaData.iYear =  Convert.ToInt32(sxRowSplitted[1]);
                continue;
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KSLA.DELIVERED_DATE))
            {
                slaData.dtmDeliveredDate = Convert.ToDateTime(sxRowSplitted[1]);
                continue;
            }
            else if (sxRowSplitted[0].ToUpper().Contains(KSLA.FILEUPLOAD))
            {
                slaData.iFile_Upload_ID = Convert.ToInt32(sxRowSplitted[1]);
                continue;
            }
            else
            {
                continue;
            }
        }

        DBQuery.SLATable.SaveSLA(slaData).ToString();
        sxReturn = SLAManager.BuildEditor_SLAData();
        return sxReturn;
    }

    #endregion Custom Methods
}
